<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Food as FoodModel;
use app\api\model\FoodCategory as FoodCategoryModel;

/**
 * 示例接口
 */
class Food extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['index'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['index'];

    public function index()
    {
        if ($this->request->isPost()) {
            //页数
            $pageNum = $this->request->param('pageNum', 1);
            //显示数量
            $pageSize = $this->request->param('pageSize', 10);
            //关键词
            $keyword = $this->request->param('keyword', '');
            //返回字段
            $field = 'id as deal_id,image as deal_img,value,price,name as deal_name,description as deal_desc,phones as deal_phones';
            //实例化美食模型
            $food = new FoodModel();
            //进行查询
            if (!empty($keyword)) {
                $food->where('name', 'LIKE', "%$keyword%");
            }
            $list = $food->field($field)->page($pageNum, $pageSize)->select();
            $this->success('查询成功', [
                'rows' => $list,
            ]);
        } elseif ($this->request->isGet()) {
            //分类名称
            $cat = $this->request->param('cat', '');
            //美食id
            $id = $this->request->param('deal_id', '');

            if (!empty($cat) && !empty($id)) {
                $this->error('不许同时携带两个参数');
            }

            if (!empty($cat)) {
                $food_category = FoodCategoryModel::get(['name' => $cat]);
                if (empty($food_category)) {
                    $this->error('未找到分类');
                }
                // dump($food_category);
                $result = [];
                $foods = $food_category->foods;
                // dump($res);
                if (!empty($foods)) {
                    foreach ($foods as $key => $food) {
                        $result[] = [
                            'deal_id' => $food->id,
                            'deal_img' => $food->image,
                            'deal_seller' => $food->dseller,
                            'value' => $food->value,
                            'price' => $food->price,
                            'deal_name' => $food->name,
                            'deal_desc' => $food->description,
                            'deal_phones' => $food->phones,
                        ];
                    }
                }

                $this->success('查询成功', [
                    'rows' => $result,
                ]);
            } elseif (!empty($id)) {
                $food = FoodModel::get($id);
                if ($food) {
                    $result = [
                        'deal_id' => $food->id,
                        'deal_img' => $food->image,
                        'deal_seller' => $food->dseller,
                        'value' => $food->value,
                        'price' => $food->price,
                        'deal_name' => $food->name,
                        'deal_desc' => $food->description,
                        'deal_phones' => $food->phones,
                    ];
                    $this->success('查询成功', $result);
                }
                $this->error('未找到美食');
            }
        }
    }
}
